using Core;

namespace g_gongjianta
{
    public partial class SenseComponent : Core.Component, IAwake, IAwake<string>
    {
        public Sense Attack { get; private set; }
        private bool isAnchor;

        public void Awake()
        {
            isAnchor = false;
            Attack = G.Factory.Create<Sense>();
            AddEventListener();
        }

        public void Awake(string code)
        {
            isAnchor = true;
            Attack = G.Get<SenseSystem>().GetSense(code);
            AddEventListener();
        }


        public override void Dispose()
        {
            if (IsDisposed) return;

            RemoveEventListener();
            if (isAnchor)
            {
                Attack.Dispose();
            }

            Attack = null;

            base.Dispose();
        }

        private void AddEventListener()
        {
            Attack.OnChanged += OnAttackChangedHandler;
        }

        private void RemoveEventListener()
        {
            Attack.OnChanged -= OnAttackChangedHandler;
        }

        private void OnAttackChangedHandler()
        {
            if (Attack.IsOn)
            {
                Mark(ESkillLinkType.Attack);
            }
            else
            {
                Unmark(ESkillLinkType.Attack);
            }
        }
    }
}